<?php
namespace Addons\Wukong\Model;

use Think\Model;

/**
 * Class WukongConversationModel   悟空会话模型
 * @package Addons\Wukong\Model
 * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
 */
class WukongConversationModel extends WukongModel
{

    /**
     * createConversation  创建会话
     * @param null $creator
     * @param array $member
     * @param string $title
     * @return array
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    public function createConversation( $creator = null, $member = array(),$title='聊天')
    {
        $creator = $creator == null ? is_login() : $creator;
        $member = is_array($member)?$member:array($member);
        $url = $this->_host . '/v1/im/conversation/create';

        array_unshift($member, $creator); //在成员里面加上创建者
        $array = array(
            'openId' => $creator,
            'type' => count($member) > 2 ? 2 : 1, //超过两个人为群聊
            "icon" => "",
            "title" => $title, //群名称
            "members" => $member,
            "tag" => 100,
            "extension" => array(
                "key1" => "value1",
                "key2" => "value2",
                "key3" => "value3"
            )
        );

        $json = json_encode($array);
        $res = $this->visitUrl($url, 'POST', $json);
        return $res;
    }



    public function getConversationList($openId = null,$cursor=0 ,$count = 10){
        $url = $this->_host . '/v1/im/conversation/list ';
        $openId = $openId == null ? is_login() : $openId;

        $array = array(
            'openId' => $openId,
            'cursor' => $cursor,
            'count' => $count
        );

        $params = http_build_query($array);
        $res = $this->visitUrl($url . '?' . $params, 'GET');
        return $res;

    }


    public function getConversation($conversation_id,$openId=null){
        $url = $this->_host . '/v1/im/conversation/profiles/get';
        $openId = $openId == null ? is_login() : $openId;
        $conversation_id = is_array($conversation_id)?$conversation_id:array($conversation_id);
        $array = array(
            'openId' => $openId,
            'conversationIds' => $conversation_id,
        );

        $json = json_encode($array);
        dump($json);
        $res = $this->visitUrl($url, 'POST',$json);
        return $res;
    }

}